package com.qiyu.constant;

import lombok.Getter;

import java.util.Arrays;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 上下移动，置顶操作类型
 *
 * @author zww
 */
@Getter
public enum MoveTypeEnum {
    /**
     * 上移
     */
    UP(1, "上移"),
    /**
     * 下移
     */
    DOWN(-1, "下移"),
    /**
     * 置顶
     */
    TOP(2, "置顶"),
    /**
     * 置尾
     */
    LAST_TOP(-2, "置尾");
    
    
    private Integer type;
    
    
    private String name;
    
    MoveTypeEnum(Integer type, String name) {
        this.type = type;
        this.name = name;
        Cache.cacheMap.put(type, this);
    }
    
    public static MoveTypeEnum getInstance(Integer type) {
        AtomicReference<MoveTypeEnum> moveTypeEnum = new AtomicReference<>(Cache.cacheMap.get(type));
        if (moveTypeEnum.get() == null) {
            Arrays.stream(MoveTypeEnum.values()).forEach(moveTypeEnum1 -> {
                if (moveTypeEnum1.getType().equals(type)) {
                    moveTypeEnum.set(moveTypeEnum1);
                }
            });
        }
        return moveTypeEnum.get();
    }
    
    private static class Cache {
        
        private static ConcurrentHashMap<Integer, MoveTypeEnum> cacheMap = new ConcurrentHashMap<>();
    }
}
